<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;
use think\Db;
use think\Image;

//失物领取中心
class AdminMeetingController extends AdminBaseController
{

    //列表
    public function index(){
        $log_list = Db::name('meeting')
            ->order("id")
            ->paginate(11);

        // 获取分页显示
        $page = $log_list->render();
       
        $this->assign("page", $page);
  
        $this->assign("data", $log_list);
        return $this->fetch();
    }
    //添加页面
    public function add(){
        return $this->fetch();
    }
    //添加提交
    public function addPost()
    {   
        
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $post   = $data['post'];
            
            if (empty($post['list_title'])) {
                $this->error("会议列表标题不能为空！");
            }
            if (empty($post['list_subtitle'])) {
                $this->error("会议列表副标题不能为空！");
            }

            $pattern = '/^\d+(\.\d+)?$/';
            if(!preg_match($pattern,$post['in_price'])){
                $this->error("对内价格必须填数字！");
            }
            if(!preg_match($pattern,$post['out_price'])){
                $this->error("对外价格必须填数字！");
            }
            $res = Db::name('meeting')->insert($post);
            if ($res) {
                //操作日志
                admin_log($post['list_title'], 'add', '会议室');

                $this->success('添加成功!', url('AdminMeeting/index'));
            }else{
                $this->error('添加失败');
            }
            
        }

    }
    //编辑
    public function edit(){
        $id = $this->request->param('id', 0, 'intval');
        $data = Db::name('meeting')->find($id);
        $this->assign('result',$data);
        return $this->fetch();
    }
    //编辑提交
    public function editPost(){
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $post   = $data['post'];
            

            if (empty($post['list_title'])) {
                $this->error("会议列表标题不能为空！");
            }
            if (empty($post['list_subtitle'])) {
                $this->error("会议列表副标题不能为空！");
            }
            $pattern = '/^\d+(\.\d+)?$/';
            if(!preg_match($pattern,$post['in_price'])){
                $this->error("对内价格必须填数字！");
            }
            if(!preg_match($pattern,$post['out_price'])){
                $this->error("对外价格必须填数字！");
            }
            
           
            $res = Db::name('meeting')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                //操作日志
                admin_log($post['list_title'], 'edit', '会议室');

                $this->success('编辑成功!', url('AdminMeeting/index'));
            }else{
                $this->error('编辑失败');
            }
            
        }
    }
    //删除提交
    public function delete(){
        $param   = $this->request->param();
        if (isset($param['id'])) {
            $id   = $this->request->param('id', 0, 'intval');
            $data = Db::name('meeting')->find($id);
            $res = Db::name('meeting')->where(['id'=>$id])->delete();
            if ($res) {
                //操作日志
                admin_log($data['name'], 'delete', '会议室');

                $this->success('删除成功!', url('AdminMeeting/index'));
            }else{
                $this->error('删除失败');
            }
        }
    }
    //会议室使用文章
    public function article(){
        $meeting_article  = cmf_get_option('meeting_article');
        $this->assign('meeting_article',$meeting_article);
        return $this->fetch();
    }
    //文章编辑
    public function articlePost(){

        if ($this->request->isPost()) {
            //TODO 非空验证
            $meeting_article = $this->request->post();

            cmf_set_option('meeting_article', $meeting_article);

            //操作日志
            admin_log('','edit','会议室使用文章');

            $this->success('保存成功！');
        }
    }
}
